package com.bprogram.config;


import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import org.springframework.mock.web.MockHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
//跨模块调用时才会执行此拦截器，而且这个拦截器比我们自定义的jwtfilter先执行。
//解决跨模块调用时，丢失token问题和定时任务不存在request的问题
@Configuration
public class FeignConfig implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {

//        // 在您的代码中获取JWT令牌的逻辑
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        assert attributes != null;
//
//        HttpServletRequest request = attributes.getRequest();
//        String token = request.getHeader("Authorization");
//        // 从当前请求的上下文中获取JWT令牌
////        String token = // 获取JWT令牌的逻辑，例如从请求的Header中获取
//        System.out.println("我是feignToken  我被调用了"+token);
//        if (token != null) {
//            // 将JWT令牌添加到Feign请求的Header中
//            template.header("Authorization", token);
//
//
//        }

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            String token = request.getHeader("Authorization");
            // 从当前请求的上下文中获取JWT令牌
//        String token = // 获取JWT令牌的逻辑，例如从请求的Header中获取
            System.out.println("我是feignToken  我被调用了" + token);
            if (token != null) {
                // 将JWT令牌添加到Feign请求的Header中
                template.header("Authorization", token);
            }
        } else {
            if (attributes == null) {
                RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(new MockHttpServletRequest()));//为空进行新建
                attributes = (ServletRequestAttributes) RequestContextHolder
                        .getRequestAttributes();//重新赋值
            }
            HttpServletRequest request = attributes.getRequest();
            // 对消息头进行配置
            Enumeration<String> headerNames = request.getHeaderNames();

            if (headerNames != null) {
                /**
                 * 判断有没有token，如果是定时任务进来，是没有token的，此时用默认token以调用到对应的Feign服务
                 */
                boolean flag = true;
                while (headerNames.hasMoreElements()) {
                    String name = headerNames.nextElement();
                    String values = request.getHeader(name);
                    if (name.equals("Authorization")) {
                        flag = false;
                    }
                    template.header(name, values);
                }
                if (flag) {
                    template.header("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlIjoic3VwZXIiLCJ1c2VyQ29kZSI6IjEyMzQ1NiJ9.QLDxcW7HZzVZK0zMiQhw9nbuXNoW4QnGJFUjoN9xn6s");
                }
            }
            // 对请求体进行配置
            Enumeration<String> bodyNames = request.getParameterNames();
            StringBuffer body = new StringBuffer();
            if (bodyNames != null) {
                while (bodyNames.hasMoreElements()) {
                    String name = bodyNames.nextElement();
                    String values = request.getParameter(name);
                    body.append(name).append("=").append(values).append("&");
                }
            }
            if (body.length() != 0) {
                body.deleteCharAt(body.length() - 1);
                template.body(body.toString());
            }
        }
    }
}

